const Web3 = require('web3')
const Token = require('../Token.json')

const ganachePrivateKey =
  '0x700c472fef576757270df5068e0ec7152e7307000cfe3fbf5d15be43bde55230'

// 连接provider
let local_provider = new Web3.providers.HttpProvider('http://localhost:8545')

// 构造web3
var web3 = new Web3(local_provider)

// 账户
let wallet = web3.eth.accounts.wallet.add(ganachePrivateKey)
console.log(`Loaded wallet ${wallet.address}`)

// 合约
const abi = Token.abi
const bytecode = Token.bytecode

async function deploy() {
  // 合约
  var myContract = new web3.eth.Contract(abi)
  // 部署合约 传入参数
  let contractAddress = ''
  await myContract
    .deploy({
      data: bytecode,
      arguments: [10000],
    })
    .send({
      from: wallet.address,
      gas: 1500000,
    })
    .then(function (newContractInstance) {
      contractAddress = newContractInstance.options.address

      console.log('智能合约地址:', newContractInstance.options.address) // 新地址的合约实例
    })
  console.log('------合约部署成功')
  return contractAddress
}

deploy()
